package cn.hn3l.agriculture.controllers.manager;

import cn.hn3l.agriculture.entitis.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author Wang Weiwei
 * @Since 16-10-15
 * @Describe
 */
@Controller
@RequestMapping("/manager")
public class ManagerController {
    @RequestMapping
    public String managerIndex(){
        return "manager/index";
    }

    //produces = "application/json;charset=UTF-8"解决传输json乱码问题
    @ResponseBody
    @RequestMapping(value = "/loginCheck"
                    ,produces = "application/json;charset=UTF-8")
    public String loginCheck(@Valid User user, BindingResult bindingResult,
                             HttpServletResponse response) throws UnsupportedEncodingException {
        List<ObjectError> allErrors = bindingResult.getAllErrors();
        System.out.println(allErrors.get(0).getDefaultMessage());
        return "{error:[{errorinfo:"+allErrors.get(0).getDefaultMessage() +"}]}";
    }
}
